/*
 * Copyright (C) 2010 Raimundo Mirisola (raimundo.mirisola@gmail.com)
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */

package com.onerm.ui;

import android.app.Activity;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class RMTable extends TableLayout {

  private static final double[] PERCENTILES = {
    0.50, .55, .60, .65, .70, .75, .80, .85, .90, .95};

  private final String[] REPS = {
      "20+",
      "18-20",
      "16-18",
      "14-15",
      "12",
      "10",
      "8",
      "6",
      "5",
      "2-3"
  };

  private final TextView[] RMText = new TextView[PERCENTILES.length];

  public RMTable(Activity context) {
    super(context);

    final int[] color = {Color.rgb(49, 64, 133), Color.rgb(26, 35, 74)};

    // table header.
    {
      TableRow row = new TableRow(context);
      row.setBackgroundColor(color[0]);
      row.addView(PanelBuilder.newTextView(context, "Desired #reps"));
      View tv = PanelBuilder.newTextView(context, "Weight");
      tv.setPadding(100, 0, 0, 0);
      row.addView(tv);
      row.setPadding(2, 2, 2, 2);
      this.addView(row);
    }

    for (int i = 0; i < PERCENTILES.length; ++i) {
      TableRow row = new TableRow(context);
      row.setBackgroundColor(color[(i+1) % 2]);
      row.setPadding(2, 2, 2, 2);
      row.addView(PanelBuilder.newTextView(context, REPS[i]));
      RMText[i] = new TextView(context);

      row.addView(RMText[i]);
      RMText[i].setGravity(Gravity.RIGHT);

      LayoutUtils.Layout.WidthFill_HeightWrap.applyTableLayoutParams(row);
      this.addView(row);
    }
    LayoutUtils.Layout.WidthFill_HeightWrap.applyViewGroupParams(this);
  }

  public void updateOneRM(double oneRM) {
    for (int i = 0; i < PERCENTILES.length; ++i) {
      RMText[i].setText(String.format("%.2f", oneRM * PERCENTILES[i]));
    }
  }
}
